<?php
require 'includes/config.php';
require 'includes/auth.php';
require 'includes/sanitize.php';

$accesstoken = $_SESSION['accesstoken'];
$data = sanitize(&$_POST);
// Check if there is some league to create
if (isset($data['submit']) && $data['name'] != "" && isset($data['description']) && isset($data['moneylimit']) 
&& isset($data['draftlimit']) && isset($data['securitylevel'])) {
	$owner = $_SESSION['uid'];
	$msg="";
	$level = 0;
	// Resolve the securitylevel to a value
	switch ($data['securitylevel']){
		case "Open for all":
			$level = 1;
			break;
		case "Friends only":
			$level = 2;
			break;
		case "Invite only":
			$level = 3;
			break;
	}
	// Try to post the league to the backend
	$request = new makeRequest($backend.'api.php', 'POST', 'method=league&name='.$data['name'].
	'&description='.$data['description'].'&moneylimit='.$data['moneylimit'].'&draftlimit='.$data['draftlimit'].
	'&playerlimit='.$data['playerlimit'].'&securitylevel='.$level.'&accesstoken='.$accesstoken.'&auid='.$owner);
	$tmp = $request->makeRequest();
	$rbody = $tmp[0];
	$http_code = $tmp[1];
	// Check for errors
	if($http_code == 200){
		$msg="League created!";
	}else{
		$msg="Could not create league ".$http_code;
	}
}
require 'includes/header.php';
require 'includes/topmenu.php';
echo '<div id="kaka">';
require 'includes/sidemenu.php';
echo '<div id="content">';
if($msg == "League created!"){
	echo $msg;
}else
{
	?>

<div id="info">
<table>
	<form method="post" action="<?php echo $PHP_SELF; ?>">
	<tr>
		<td>League name:<br />
		</td>
		<td><input type="text" name="name"
			value="<?php echo $data['name']; ?>" /><?php echo $msg?><br />
		</td>
	</tr>
	<tr>
		<td>Description: <br />
		</td>
		<td><input type="text" name="description"
			value="<?php echo $data['description']; ?>" /><br />
		</td>
	</tr>
	<tr>
		<td>Money limit: <br />
		</td>
		<td><select name="moneylimit">
		<?php if(isset($data['moneylimit'])){
			echo '<option id="old">'.$data["moneylimit"].'</option>';
		} ?>
			<option id="10000">10000</option>
			<option id="15000">15000</option>
			<option id="20000">20000</option>
			<option id="25000">25000</option>
			<option id="30000">30000</option>
			<option id="unlimited">999999</option>
		</select></td>
	</tr>
	<tr>
		<td>Player limit: <br />
		</td>
		<td><select name="playerlimit">
		<?php if(isset($data['playerlimit'])){
			echo '<option id="old">'.$data["playerlimit"].'</option>';
		} ?>
			<option id="5">5</option>
			<option id="10">10</option>
			<option id="15">15</option>
			<option id="20">20</option>
			<option id="30">30</option>
			<option id="unlimited">999999</option>
		</select></td>
	</tr>
	<tr>
		<td>Number of draft during season limit: <br />
		</td>
		<td><select name="draftlimit">
		<?php if(isset($data['draftlimit'])){
			echo '<option id="old">'.$data["draftlimit"].'</option>';
		} ?>
			<option id="5">5</option>
			<option id="10">10</option>
			<option id="15">15</option>
			<option id="20">20</option>
			<option id="30">30</option>
			<option id="unlimited">999999</option>
		</select></td>
	</tr>
	<tr>
		<td>Security level: <br />
		</td>
		<td><select name="securitylevel">
		<?php if(isset($data['securitylevel'])){
			echo '<option id="old">'.$data["securitylevel"].'</option>';
			echo '<option id="null"></option>';
		} ?>
			<option id="Open for all">Open for all</option>
			<option id="Friends only">Friends only</option>
			<option id="Invite onlys">Invite only</option>
		</select></td>
	</tr>
	<tr>
		<td><input type="submit" Value="Submit" name="submit" />
	
	</form>
	</td>
	<td></td>
	</tr>
</table>
		<?php
		echo '</div>';
}
require 'includes/footer.php';
?>

</body>
</html>